﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kale.Core.Security;

namespace Kale.Db.Sql
{
   internal static class SecurityLoader
   {
      internal static void LoadEffectiveRoles(ISecurityMember member, List<IUserRole> roles)
      {
         foreach (IUserRole role in member.ExplicitRoles)
         {
            roles.Add(role);
         }

         foreach (IUserGroup group in member.EffectiveUserGroups)
         {
            foreach (IUserRole role in group.ExplicitRoles)
            {
               if (!roles.Contains(role)) roles.Add(role);
            }
         }
      }

      internal static void LoadEffectiveGroups(ISecurityMember member, List<IUserGroup> groups)
      {
         RecurseGroups(member, groups);
      }

      private static void RecurseGroups(ISecurityMember  member, List<IUserGroup> groups)
      {
         foreach (IUserGroup group in member.ExplicitUserGroups)
         {
            if (!groups.Contains(group)) groups.Add(group);

            RecurseGroups(group, groups);
         }
      }
   }
}
